package main

import "fmt"

/* 26. 删除有序数组中的重复项 https://leetcode.cn/problems/remove-duplicates-from-sorted-array/*/

func ExecQ26() {
	num1 := []int{1, 1, 2}
	fmt.Println(removeDuplicates(num1), num1)

	num2 := []int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}
	fmt.Println(removeDuplicates(num2), num2)
}

// 我写的
func removeDuplicates1(nums []int) int {
	count := 0
	for i := 0; i < len(nums); i++ {
		if nums[i] != nums[count] {
			count++
			nums[count] = nums[i]
		}
		if count != i {
			nums[i] = 0
		}
	}
	return count + 1
}

// 官方的 ，结果不对啊 ~~~
func removeDuplicates(nums []int) int {
	n := len(nums)
	if n == 0 {
		return 0
	}
	slow := 1
	for fast := 1; fast < n; fast++ {
		if nums[fast] != nums[fast-1] {
			nums[slow] = nums[fast]
			slow++
		}
	}
	return slow
}
